//
//  EditAddressViewController.m
//  LoveFreshBeen
//
//  Created by 胡不归是大虾 on 2016/10/21.
//  Copyright © 2016年 魏根. All rights reserved.
//

#import "EditAddressViewController.h"
#import "addressEditCell.h"
#import "genderChooseCell.h"
#import "MyAddressViewModel.h"
@interface EditAddressViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,weak) UITableView* tableView;

@end

@implementation EditAddressViewController
-(instancetype)init{
    if (self = [ super init]) {
        self.hidesBottomBarWhenPushed = YES;
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.rowHeight = 40;
    [self addBackButton];
    [self.navigationController.navigationBar setBarTintColor:[UIColor whiteColor]];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"保存" style:UIBarButtonItemStylePlain target:self action:@selector(didClickSaveBtn)];
    
    [self.navigationItem.rightBarButtonItem setTintColor:[UIColor lightGrayColor]];
    
//    self.navigationItem.rightBarButtonItem.enabled = NO;
}
-(void)didClickSaveBtn{
    if (self.saveBlock) {

        self.saveBlock(self.model);
    }
    [self.navigationController popViewControllerAnimated:YES];

    [SVProgressHUD setMinimumDismissTimeInterval:0.5];
    [SVProgressHUD setDefaultMaskType:SVProgressHUDMaskTypeBlack];
    [SVProgressHUD showSuccessWithStatus:@"修改成功"];
    
    
}
-(UITableView *)tableView{
    if (_tableView == nil) {
        UITableView* tableView = [[UITableView alloc] initWithFrame:self.view.bounds style:UITableViewStyleGrouped];
        tableView.delegate = self;
        tableView.dataSource = self;
        tableView.tableFooterView = [UIView new];
        _tableView = tableView;
        [self.view addSubview:tableView];
    }
    return _tableView;
}
-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return  self.model.accept_name == nil ? 1 : 2;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 1) {
        return 1;
    }
    else{
        return 6;
    }
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if ( indexPath.section == 0) {
       if( indexPath.row == 1)
       {
        genderChooseCell* cell = [[[NSBundle mainBundle] loadNibNamed:@"genderChooseCell" owner:nil options:nil] lastObject];
        cell.model = self.model;
        return cell;
       }else{
           addressEditCell* cell = [[[NSBundle mainBundle] loadNibNamed:@"addressEditCell" owner:nil options:nil] lastObject];
           cell.row = indexPath.row;
           cell.model = self.model;
           return cell;
       }
    }
    else
    {
        UITableViewCell* cell = [[UITableViewCell alloc] init];
        cell.textLabel.text = @"删除当前地址";
        cell.textLabel.font = [UIFont systemFontOfSize:16];
        return cell;
    }
}

-(MyAdressModel *)model{
    if (!_model) {
        _model = [[MyAdressModel alloc] init];
    }
    return _model;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 1) {
        [[MyAddressViewModel sharedAdressViewModel].modelArray removeObject:self.model];
        [self.navigationController popViewControllerAnimated:YES];
    }
}

- (void)addBackButton {
    
    self.title = @"修改地址";
    UIButton *back = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, 30, 30)];
    
    [back setBackgroundImage:[UIImage imageNamed:@"v2_goback"] forState:UIControlStateNormal];
    
    [back addTarget:self action:@selector(back) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:back];
}


- (void)back {
    
    [self.navigationController popViewControllerAnimated:YES];
    
}






@end
